import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { TableTemplate } from '../entities/template.entity';
@Injectable()
export class TableTemplateService {
  constructor(
    @InjectRepository(TableTemplate)
    private readonly templateRepository: Repository<TableTemplate>,
  ) {}

  async getTemplates(
    orderBy: 'ASC' | 'DESC' = 'DESC',
  ): Promise<TableTemplate[]> {
    return await this.templateRepository.find({
      order: {
        createTime: orderBy,
      },
    });
  }
  async createTemplate(
    template: Partial<TableTemplate>,
  ): Promise<TableTemplate> {
    const newTemplate = this.templateRepository.create(template);
    return await this.templateRepository.save(newTemplate);
  }
  async createTemplates(
    templates: Partial<TableTemplate>[],
  ): Promise<TableTemplate[]> {
    const newTemplates = this.templateRepository.create(templates);
    return await this.templateRepository.save(newTemplates);
  }
}
